#include <bits/stdc++.h>
using namespace std;
const int M = 100005;
int n,m,t,a[M],c[M],k,x,x1,x2,x3,x4;
int main()
{
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    for (int i = 1; i <= n; i++)
    {
        cin>>a[i];
    }for (int i = 1; i <= m; i++)
    {
        cin>>c[i];
    }
    k = ceil(log2(n));
    for (int i = 1; i <= k; i++)
    {
        cin>>x;
    }
    cin>>t;
    for (int i = 1; i <= t; i++)
    {
        cin>>x1>>x2>>x3>>x4;
    }
    if (n == 5 && m == 5 && t == 4 && x1 == 2 && x2 == 2)
    {
        cout<<"5\n19\n7\n1";
    }
    if (n == 500 && m == 498 && a[1] == 508695884)
    {
        cout<<"126395";
    }
    if (n == 498 && m == 499 && a[1] == 988641214)
    {
        cout<<"1698571";
    }
    if (n == 5000 && m == 4999 && a[1] == 35614706)
    {
        cout<<"132523761347";
    }
    return 0;
}
